package cn.david.org.service.impl;


import cn.david.basic.exception.BusinessExcepiton;
import cn.david.basic.service.impl.BasicServiceImpl;
import cn.david.org.domain.Employee;
import cn.david.org.domain.Shop;
import cn.david.org.mapper.EmployeeMapper;
import cn.david.org.mapper.ShopMapper;
import cn.david.org.service.IEmployeeService;
import cn.david.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.nio.BufferUnderflowException;
import java.util.prefs.BackingStoreException;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BasicServiceImpl<Shop> implements IShopService{
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IEmployeeService employeeService;

    @Override
    public void deleteByIds(Long[] ids) {
        shopMapper.deleteByIds(ids);

    }

    @Override
    public void settlement(Shop shop) {
        //效验是否存在
        Employee admin = shop.getAdmin();
        Employee employeeobj = employeeMapper.loadByUser(admin);
        if (employeeobj!=null){
            throw new BusinessExcepiton("店铺已经注册，请直接登录");
        }
        employeeService.add(admin);
        shop.setAdmin(admin);
        shop.setState(1);
        shopMapper.save(shop);
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
    }
}
